<?php

/*
Autor: Imanol Iglesias
email: imanolif@gmail.com
*/

define('URL_SMS', 'https://opensms.movistar.es/aplicacionpost/loginEnvio.jsp');

include_once("Request.php");

class EnvioSMS
{
	private $login;
	private $password;

	public function __construct($login, $password)
	{
		$this->login = $login;
		$this->password = $password;
	}
	
    public function enviar($destino, $mensaje)
    {
    	$request = new Request(URL_SMS);
		$request->setOpt(CURLOPT_SSL_VERIFYPEER, false);
		$request->setOpt(CURLOPT_SSL_VERIFYHOST, false);
		$request->setOpt(CURLOPT_HEADER, true);
		$request->setOpt(CURLOPT_FOLLOWLOCATION, false);
		$request->setOpt(CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
		$request->setOpt(CURLOPT_RETURNTRANSFER, true);
		$request->setUserAgent('firefox');
		
		$postdata = 'TM_ACTION=AUTHENTICATE&TM_LOGIN='.$this->login.'&TM_PASSWORD='.$this->password.'&to='.$destino.'&message='.$this->sinAcentos($mensaje);
		
		$request->setPOST($postdata);	
		
        $header = array(
			'Content-Type: application/x-www-form-urlencoded',
    		'Content-Length: '.strlen($postdata),
    		'Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*',
    		'Connection: Keep-Alive'
			);
        $request->setHeader($header);
        
        $resultado = $request->exec();
        
        //probando
		echo $request->getResult();
    }
    
    private function sinAcentos($mensaje)
    {
		$quitar = "ÀÁÂÃÄÅàáâãäåÒÓÔÕÖØòóôõöøÈÉÊËèéêëÇçÌÍÎÏìíîïÙÚÛÜùúûüÿÑñ";
		$poner = "AAAAAAaaaaaaOOOOOOooooooEEEEeeeeCcIIIIiiiiUUUUuuuuyNn";
		return(strtr($mensaje, $quitar, $poner));
    }
    
    public function enviarSMS($sms)
    {
    	$this->enviar($sms->getDestino(), $sms->getTexto());
    }

}

?>